<!--
 * @Author: dengyl
 * @Date: 2022-01-11 19:54:44
 * @LastEditTime: 2022-08-19 16:55:12
 * @LastEditors: 代琪 1529992814@qq.com
 * @Description: 新闻-卡片组件
 * @FilePath: \code\ctp-static-portal-front\src\components\news\components\card.vue
-->
<template>
  <div class="news-card" :style="themStyle" ref="container">
    <!--多图平铺1样式-->
    <div v-if="cardType === 'two'" ref="twoDom">
      <TypeTwo
        :dataList="dataList"
        :isOne="isOne"
        :contentWidth="contentWidth"
        :options="options"
      />
    </div>
    <!--卡片式样式-->
    <template v-else>
      <ul class="type-default" v-for="(d, i) in dataList" :key="i">
        <li
          v-for="(item, i) in d"
          :key="item.link"
          :style="i === d.length - 1 ? `width:${columnWidth - 13}px` : liStyle"
        >
          <a :href="`${newsBasePath}${item.link}`" target="_blank">
            <!-- <img :src="picSrc + item.imageUrl" /> -->
            <ShowImg :item="item" />
          </a>
          <div class="card-info">
            <a
              :href="`${newsBasePath}${item.link}`"
              :title="item.subject"
              class="title"
              target="_blank"
              >{{ item.subject }}</a
            >
            <div class="detail">{{ item.brief }}</div>
            <div class="info">
              <span v-if="options.rowList && options.rowList.includes('publishDate')"
                ><span>{{ item.createDate }}</span></span
              >
              <!-- <span>阅读量：{{ item.viewNum }}</span> -->
            </div>
          </div>
        </li>
      </ul>
      <div class="switch" @click="cardSwitch(++cardIndex)"
        ><span class="vportal vp-change-one-change"></span>换一换</div
      >
    </template>
  </div>
</template>

<script>
  import TypeTwo from './card-type-two.vue';
  import { methods } from '@/utils';
  export default {
    name: 'NewsCard',
    components: {
      TypeTwo
    },
    props: {
      cardType: { type: String },
      list: { type: Array },
      options: { type: Object },
      contentWidth: { type: Number, default: 0 }
    },
    data() {
      return {
        picSrc,
        // 一共需要展示的数据
        dataList: [],
        newsBasePath: `${_basePath}/`,
        liStyle: {},
        rowCount: 0, //一行多少数据
        dataNumber: 0, //一页多少条数据
        //窄栏目用，是否展示窄栏目
        isOne: this.contentWidth <= 480,
        //当前索引
        cardIndex: 0,
        //每一列宽度
        columnWidth: 0
      };
    },
    computed: {
      themStyle() {
        return {
          background: this.options.backgroundColor
        };
      }
    },
    created() {
      //只有一个
      if (this.isOne) {
        this.rowCount = 1;
      } else {
        const { width, count } = methods.getColumnData(this.contentWidth - 10);
        this.rowCount = count;
        this.columnWidth = width;
      }
      //生成数据
      this.dataNumber = this.rowCount * this.options.rowsNum;
      this.cardSwitch();
    },
    mounted() {
      if (this.isOne) {
        //设置宽度
        this.liStyle = {
          width: `${this.$refs.container.clientWidth - 12}px`
        };
      } else {
        this.liStyle = {
          //减去左右margin 5 border 1
          width: `${this.columnWidth - 12}px`
        };
      }
    },
    methods: {
      cardSwitch(index = 0) {
        const list = this.list.slice(index * this.dataNumber, (index + 1) * this.dataNumber);
        //数量超出就直接回到第一页
        if (!list.length) {
          this.cardSwitch((this.cardIndex = 0));
          return;
        }
        const dataList = [];
        while (list.length) {
          dataList.push(list.splice(0, this.rowCount));
        }
        this.dataList = dataList;
      }
    }
  };
</script>
<style lang="scss" scoped>
  .news-card {
    > ul {
      padding: 0;
      margin: 0;
      list-style: none;

      > li {
        vertical-align: top;
      }
    }

    .title {
      display: block;
      overflow: hidden;
      text-overflow: ellipsis;
      white-space: nowrap;
    }

    > .type-default {
      > li {
        display: inline-block;
        width: 284px;
        height: 285px;
        margin: 5px;
        background-color: #fff;
        border: 1px solid #e0e0e0;
        border-radius: 4px;

        > a {
          display: block;
          width: 100%;
          height: 170px;

          > img {
            width: 100%;
            height: 100%;
          }
        }

        > .card-info {
          padding: 5px 10px 10px 10px;
          // border: 1px solid #e0e0e0;
          border-top: none;

          > a:hover {
            color: #1890ff;
          }

          .detail {
            display: -webkit-box;
            max-height: 32px;
            padding: 5px 0 0 0;
            overflow: hidden;
            font-size: 12px;
            line-height: 16px;
            color: #666;
            -webkit-box-orient: vertical;
            -webkit-line-clamp: 2;
          }

          .info {
            margin-top: 10px;
            font-size: 12px;
            color: #999;

            > span {
              margin-right: 10px;
            }
          }
        }
      }
    }

    > .type-two {
      > li {
        position: relative;
        display: inline-block;
        width: 47%;
        height: 120px;
        margin: 0 5px 10px 0;
        border: 1px solid #e0e0e0;
        border-radius: 4px;

        > a {
          display: block;
          width: 100%;
          height: 100%;

          > img {
            width: 100%;
            height: 100%;
          }
        }

        > div {
          position: absolute;
          bottom: 0%;
          width: 100%;
          height: 30px;
          line-height: 30px;
          color: #fff;
          background: rgba(0, 0, 0, 0.3);
          border-radius: 0 0 5px 5px;

          > a {
            margin: 0 10px;
          }
        }
      }

      > .type-two-li.type-two-li {
        width: 260px;
        height: 150px;
        margin-right: 20px;
      }
    }

    > .switch {
      color: #666;
      text-align: center;
      cursor: pointer;

      > span {
        margin-right: 5px;
        color: #999;
      }
    }
  }
</style>
